/*
 * VueDetail.java
 *
 * Created on 15 novembre 2007, 13:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package images;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;


public class VueDetail extends JPanel{
    Fichier[] monTab;
    JTable monTableau;
    TabsDeDroite leModel;
    
    
    public VueDetail(final TabsDeDroite leModel,final Fichier[] monTab) {
        this.monTab = monTab;
        this.leModel = leModel;
        
        
        setLayout(new BorderLayout());
        TableModel monModel = new DetailTableModel(monTab);
        
        monTableau = new JTable(monModel);
        
        JTableHeader leHaut = monTableau.getTableHeader();
        this.add(leHaut,BorderLayout.NORTH);
        
        JScrollPane monScrollPane = new JScrollPane(monTableau);
        
        monTableau.setCellSelectionEnabled(true);
        //   monTableau.setRowHeight(xApercu);
        //   monTableau.setIntercellSpacing(new Dimension(10, 15));
        //   monTableau.setShowGrid(false);
        //  monTableau.getTableHeader().setReorderingAllowed(false);
        //  monTableau.getTableHeader().setResizingAllowed(false);
        
        monTableau.setShowHorizontalLines(false);
        monTableau.setShowVerticalLines(false);
        monTableau.setDragEnabled(true);
        
        MouseListener monMouseListener = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                    //     int index = monTableau.getSelectedRow();
                    int index = monTableau.rowAtPoint(e.getPoint());
                    
                    int numeroRetour = VueDetail.this.leModel.mesTabs.getSelectedIndex();
                    leModel.creerVueImage(monTab[index], index, VueDetail.this.getWidth(), VueDetail.this.getHeight(), numeroRetour);
                    
                }else if(SwingUtilities.isMiddleMouseButton(e)) { // middle click
                    //    int index = monTableau.getSelectedRow();
                    
                    int index = monTableau.rowAtPoint(e.getPoint());
                    int numeroRetour = VueDetail.this.leModel.mesTabs.getSelectedIndex();
                    leModel.creerVueImageBackground(monTab[index],index, VueDetail.this.getWidth(), VueDetail.this.getHeight(), numeroRetour);
                    
                }
            }
        };
        
        
        monTableau.addMouseListener(monMouseListener);
        
        
        
        KeyListener monKeyListener = new KeyListener() {
            
            public void  keyPressed(KeyEvent e) {
                int touche = e.getKeyCode();
                if (touche  == KeyEvent.VK_ENTER) {
                    int index[] = monTableau.getSelectedRows();
                    int premierPlan = 0;
                    int numeroRetour = VueDetail.this.leModel.mesTabs.getSelectedIndex();
                    for (int i = 0; i < index.length; i++) {
                        if(premierPlan == 0) {
                            leModel.creerVueImage(monTab[index[i]], index[i], VueDetail.this.getWidth(), VueDetail.this.getHeight(), numeroRetour);
                            premierPlan = 1;
                        }else{
                            leModel.creerVueImageBackground(monTab[index[i]], index[i], VueDetail.this.getWidth(), VueDetail.this.getHeight(), numeroRetour);
                        }
                    }
                    
                    
                }
            }
            
            public void keyReleased(KeyEvent e){}
            public void keyTyped(KeyEvent e){}
        };
        
        monTableau.addKeyListener(monKeyListener);
        
        
        
        
        
        
        
        
        monTableau.setColumnSelectionAllowed(false);
        monTableau.setRowSelectionAllowed(true);
        for(int i=0;i < monTableau.getColumnModel().getColumnCount(); i++) {
            
            monTableau.getColumnModel().getColumn(i).setPreferredWidth(200);
            
        }
        
        //    monTableau.setTableHeader(null);
        monTableau.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);    // pour un seul : SINGLE_SELECTION
        //   monTableau.setAutoResizeMode(monTableau.AUTO_RESIZE_OFF);
        
        this.add(monScrollPane,BorderLayout.CENTER);
        
        
        
        
        
        
        
        
        
    }
    
}



class DetailTableModel extends AbstractTableModel {
    
    Fichier[] monTab;
    
    
    
    
    public DetailTableModel(Fichier[] monTab) {
        this.monTab = monTab;
        
        
        
    }
    
    
    public Object getValueAt(int r, int c){
        
        String val = null;
        
        switch(c){
            case 0: val = monTab[r].getNom();break;
            case 1: val = String.valueOf(monTab[r].length()/1024)+"ko";break;
            case 2: val = "Type";break;
            case 3: val = String.valueOf(monTab[r].lastModified());break;
        }
        
        return val;
    }
    
    
    
    // 3 méthodes obligé à impl�ment�
    public int getColumnCount(){return 4;}
    public String getColumnName(int c) {
        String val = null;
        
        switch(c){
            case 0: val = "Nom";break;
            case 1: val = "Taille";break;
            case 2: val = "Type";break;
            case 3: val = "Date de modification";break;
        }
        
        return val;
    }
    
    public int getRowCount() {
        
        return monTab.length;
    }
    
    
    
    
    
    
    
// important, sans ça , plus d'affichage
    public Class getColumnClass(int c) {
        String test = "salut";
        return test.getClass();
    }
    
    
}


